import io.restassured.http.ContentType;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

/**
 * Created by kristina.gu on 2018/12/22.
 */

public class TesterHomeTest {

    @BeforeClass
    public static void ignoreSSL() {
        useRelaxedHTTPSValidation();

    }
    @Test
    public void topics() {
        get("https://testerhome.com/api/v3/topics.json")
                .then().body("topics[0].title", containsString("北京沙龙"));

    }

    //    断言使用gpath，Xmlpath，jsonpath等
    @Test
    public void getDemo() {
        given()
                .param("wd", "mp3")
                .param("ie", "utf-8")
                .cookie("PSTM", "1510600412")
                .cookie("BIDUPSID", "85614512151C6A21725938906A7419A2")
                .header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) " +
                        "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36")
                .get("https://www.baidu.com/s")
                .then()
                .log().all()
                .statusCode(200).body("html.head.title", equalTo("mp3_百度搜索"))
                .body("**.find{it.@class=='nums_text'}", equalTo("百度为您找到相关结果约49,200,000个"));
    }
    @Test
    public void postDemo() {
        given().when()
                .formParam("j_username", "abc")
                .formParam("j_password", "123")
                .formParam("from", "/")
                .formParam("Submit", "Sign in")
                .post("http://jenkins.testing-studio.com:8080/j_acegi_security_check")
                .then();
    }

    @Test
    public void getDemo1() {
        given()
                .param("wd", "mp3")
                .param("ie", "utf-8")
                .cookie("PSTM", "1510600412")
                .cookie("BIDUPSID", "85614512151C6A21725938906A7419A2")
                .header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) " +
                        "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36")
                .get("https://www.baidu.com/s")
                .then()
                .log().all()
                .statusCode(200).body("html.head.title", equalTo("mp3_百度搜索"))
                .body("**.find{it.@class=='nums_text'}", equalTo("百度为您找到相关结果约49,200,000个"))
                .body(hasXPath("//*[@class='nums_text' and contains(text(),'百度为您找到相关结果约49,200,000个']"));
    }

    @Test
    public void jsonPathDemo() {
        given().when().get("https://testerhome.com/api/v3/topics.json")
                .then()
                .statusCode(200)
                .body("topics.title[0]", equalTo("[北京沙龙] TesterHome 北京沙龙第 11 期，开始报名~"))
                .body("topics.size()", equalTo(23))
                .body("topics.findAll {it.title.contains('北京沙龙')}.title[0]", equalTo("[北京沙龙] TesterHome 北京沙龙第 11 期，开始报名~"));
    }


    @Test
    public void xmlPathDemo() {
        given().when().get("http://jenkins.testing-studio.com:8080/job/AllureDemo/api/xml")
                .then()
                .statusCode(200)
                .body("freeStyleProject.displayName", equalTo("AllureDemo"))
                .body("..lastBuild.number",equalTo("16"))
                .body("*.lastBuild.number",equalTo("16"))
                .body("..lastBuild.number.toFloat()",equalTo(16f))
                .body("**.find {it.name()=='lastSuccessfulBuild'}.number",equalTo("1"))
                .body("**.find {it.name()=='lastSuccessfulBuild'}.number.toInteger()",equalTo(1))
                .body("**.findAll {it.number=='1'}[-1].url",equalTo("http://jenkins.testing-studio.com:8080/job/AllureDemo/1/"))
            ;
    }


    @Test
    public void harmcrestDemo() {
        given().when().get("http://jenkins.testing-studio.com:8080/job/AllureDemo/api/xml")
                .then()
                .statusCode(200)
                .body("freeStyleProject.displayName", equalTo("AllureDemo"))
                .body("..lastBuild.number.toFloat()",greaterThanOrEqualTo(16f))
                .body("..lastBuild.number.toInteger()",greaterThanOrEqualTo(16))
                .body("..lastBuild.number.toDouble()",closeTo(16,3))
                .body("**.find {it.name()=='lastSuccessfulBuild'}.number",equalTo("1"))
                .body("**.find {it.name()=='lastSuccessfulBuild'}.number.toInteger()",equalTo(1))
//                .body("**.findAll {it.number=='1'}[-1].url",equalTo("p://jenkins.testing-studio.com:8080/job/AllureDemo/1/"))
        ;
    }
    @Test
    public void jsonPost() {
        HashMap<String, Object> data = new HashMap<String, Object>();
        data.put("name", "广州研发中心");
        data.put("order", 1);
        data.put("parentId", 1);
        data.put("id", 2);

        given().log().all()
                .queryParam("access_token", "XXX")
                .contentType(ContentType.JSON)
                .body(data)
                .when()
                .post("https://qyapi.weixin.qq.com/cgi-bin/department/create")
        ;
    }
    @Test
            public void xmlPost() {
        given()
                .queryParam("access_token","XXX")
                .contentType(ContentType.XML)
                .body(new DemoBean())
                .when()
                .post("https://qyapi.weixin.qq.com/cgi-bin/department/create")
                ;

    }

    @Test
    public void timeOut() {
        given().log().all()
                .when().log().all()
                .get("https://testerhome.com/api/v3/topics.json")
                .then().log().all()
                .statusCode(200)
                .time(lessThanOrEqualTo(2L), TimeUnit.SECONDS);

    }

//    public void sechame(){
//        given().get("https://testerhome.com/api/v3/topics.json")
//                .then()
//                .body(matchesJsonSchema(new File()));
//    }

//    作业一
    @Test
    public void work1(){
        given().log().all()
                .when().log().all()
                .param("ie","utf-8")
                .param("wd","霍格沃兹测试学院")
                .header("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
                .cookie("PSTM=1544770484; BD_UPN=123253; BAIDUID=188348A874B842754ED46F18A3937104:FG=1; BIDUPSID=3BAF29076A6EF118E9688DB5EA79C005; H_PS_PSSID=1433_21092_28132_26350_27751_28140_27509; delPer=0; BD_HOME=0; __lnkrntdmcvrd=-1; BD_CK_SAM=1; PSINO=5; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDSVRTM=137; WWW_ST=1545465418873")
        .get("https://www.baidu.com/s")
        .then().statusCode(200);
    }
//作业2
    @Test
    public void work2() {
        given()
                .formParam("username","123456789")
                .formParam("password","1234")
                .formParam("captcha","")
                .formParam("remember_me","true")
        .when()
                .post("https://xueqiu.com/snowman/login")
                .then()
                .statusCode(302)
            ;

    }

    @Test
    public void work3() {

        given().get("https://testerhome.com/api/v3/topics.json")
                .then()
        .statusCode(200)
        .body("topics.replies_count =='7'}",equalTo(7))
        ;
    }


}